package com.codemes.happylist.modules.transform;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import de.felixroske.jfxsupport.FXMLController;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.cell.MapValueFactory;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.util.Callback;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.ResourceBundle;

/**
 * @author nscoffee
 */
@FXMLController
@Slf4j
public class UnitTransformController implements Initializable {
    @FXML
    public TabPane transformTabPane;
    @FXML
    public AnchorPane container;

    @FXML
    public TextField tfSourcce;
    @FXML
    public ComboBox<TransformUnit> cbSourceUnit;
    @FXML
    public ComboBox<TransformUnit> cbTargetUnit;
    @FXML
    public TableView<TransformResult> tableTranform;

    @SneakyThrows
    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        transformTabPane.prefWidthProperty().bind(container.widthProperty());

        TableColumn<TransformResult,String> colunValue = new TableColumn<>("结果");
        colunValue.setCellValueFactory(new PropertyValueFactory<>("result"));
        colunValue.setPrefWidth(200);
        TableColumn<TransformResult,String> columnUnit = new TableColumn<>("单位");
        columnUnit.setPrefWidth(200);
        columnUnit.setCellValueFactory(transformResultStringCellDataFeatures -> {
            TransformUnit transformUnit = transformResultStringCellDataFeatures.getValue().getTargetUnit();
            String value = transformUnit.getName() + "(" + transformUnit.getNameEn() + ")";
            return new SimpleStringProperty(value);
        });
        tableTranform.getColumns().add(colunValue);
        tableTranform.getColumns().add(columnUnit);

        List<TransformItem> transformItemList = TransformUtil.loadItemList();
        transformItemList.forEach(item -> {
            Tab tab = new Tab();
            tab.setText(item.getName());
            tab.setId(item.getName());
            tab.setUserData(item);
            tab.selectedProperty().addListener((observableValue, aBoolean, t1) -> {
                if(Boolean.TRUE.equals(t1)){
                    cbSourceUnit.getItems().clear();
                    cbTargetUnit.getItems().clear();
                    tfSourcce.clear();
                    final List<TransformUnit> unitList = item.getUnitList();
                    if(CollUtil.isNotEmpty(unitList)){
                        unitList.forEach(unit->{
                            cbSourceUnit.getItems().add(unit);
                            cbTargetUnit.getItems().add(unit);
                        });
                    }
                }
            });

            transformTabPane.getTabs().add(tab);
        });

        cbSourceUnit.setOnAction(event -> {doTransform();});
        cbTargetUnit.setOnAction(actionEvent -> doTransform());
        tfSourcce.textProperty().addListener((observableValue, s, t1) -> doTransform());
    }

    private void doTransform() {
        Tab selectedTab = transformTabPane.getSelectionModel().getSelectedItem();
        TransformItem item = (TransformItem)selectedTab.getUserData();
        List<TransformUnit> unitList = item.getUnitList();
        TransformUnit sourceUnit = cbSourceUnit.getValue();
        TransformUnit targetUnit = cbTargetUnit.getValue();
        String inputValue = tfSourcce.getText();
        List<TransformResult> results = TransformUtil.doTransform(inputValue,sourceUnit,targetUnit,unitList);
        log.info("result ->{}",results);
        tableTranform.getItems().clear();
        tableTranform.getItems().addAll(results);

    }
}
